package com.peas.platform.module.icims.base.auth.domain.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.auth.adapter.repository.OrganizationRepository;
import com.peas.platform.module.icims.base.auth.adapter.repository.po.OrganizationPo;
import com.peas.platform.module.icims.base.auth.domain.OrganizationDomain;
import com.peas.platform.module.icims.base.auth.dto.OrganizationDto;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Component
public class OrganizationDomainImpl extends BaseDomainImpl<OrganizationDto, OrganizationPo> implements OrganizationDomain {

    @Resource
    private OrganizationRepository organizationRepository;
    @Resource
    private SequenceService sequenceService;

    @Override
    public BaseRepository<OrganizationPo> getMapper() {
        return organizationRepository;
    }

    @Override
    public Class<? extends BaseRepository<OrganizationPo>> getRepositoryClass() {
        return OrganizationRepository.class;
    }

    @Transactional
    @Override
    public int save(OrganizationDto dto) {
        if (StrUtil.isBlank(dto.getOrganizationCode())) {
            String code = sequenceService.genSequence("base", "OrganizationDto", "D", 4, 1);
            dto.setOrganizationCode(code);
        }
        int result = super.save(dto);
        this.checkRepetition("organizationCode", dto.getOrganizationCode());
        return result;
    }

    @Transactional
    @Override
    public int updateById(OrganizationDto dto) {
        int result = super.updateById(dto);
        this.checkRepetition("organizationCode", dto.getOrganizationCode());
        return result;
    }

}
